import { Component, OnInit, ViewChildren, QueryList } from '@angular/core';
import { ApplicationService } from "../../../service/application.service";
import { Farm } from "../../../entity/farm.model";
import { TurboperHistory } from "../../../entity/turboper-history.model";
import { OverhaulTableComponent } from "./overhaul-table/overhaul-table.component";
import { TurboperHistoryService } from "../../../service/turboper-history.service";
import { Device } from '../../../entity/device.model';
import { FarmCategoryEnum } from '../../common/device-tag-selector/farm-category.enum';
import { DeviceCategoryEnum } from '../../common/device-tag-selector/device-category.enum';

@Component({
  selector: 'app-farm-overhaul',
  templateUrl: './farm-overhaul.component.html',
  styleUrls: ['./farm-overhaul.component.css']
})
export class FarmOverhaulComponent implements OnInit {
  public farmCategory = FarmCategoryEnum.winfarm;
  public deviceCategory: DeviceCategoryEnum[] = [DeviceCategoryEnum.turbine];
  @ViewChildren(OverhaulTableComponent)
  overhaultables: QueryList<OverhaulTableComponent>;
  /**
   * 风场集合
   * 
   * @type {Farm[]}
   * @memberof FarmOverhaulComponent
   */
  farms: Farm[];

  /**
   * 当前风场id
   * 
   * @type {number}
   * @memberof FarmOverhaulComponent
   */
  currentFarmId: number;

  /**
   * 当前选中下标（计算点，非计算点）
   * 
   * @type {number}
   * @memberof FarmOverhaulComponent
   */
  active: number = 0;

  /**
   * 结束时间
   * 
   * @type {Date}
   * @memberof FarmOverhaulComponent
   */
  endDate: Date = new Date();

  /**
   * 开始时间
   * 
   * @type {Date}
   * @memberof FarmOverhaulComponent
   */
  startDate: Date = new Date(this.endDate.getFullYear(), this.endDate.getMonth());

  /**
   * 历史子状态集合
   * 
   * @type {TurboperHistory[]}
   * @memberof FarmOverhaulComponent
   */
  turboperHistorys: TurboperHistory[];
  turboperHistorys2: TurboperHistory[];
  currentHistory: TurboperHistory;
  /**
   * 风机id集合
   * 
   * @type {number[]}
   * @memberof FarmOverhaulComponent
   */
  deviceIdList: number[];

  /**
   * 是否显示提示框
   * 
   * @type {boolean}
   * @memberof FarmOverhaulComponent
   */
  display: boolean = false;

  /**
   * 显示编辑页面
   * 
   * @type {boolean}
   * @memberof FarmOverhaulComponent
   */
  isEdit: boolean = false;

  /**
   * 提示信息
   * 
   * @type {string}
   * @memberof FarmOverhaulComponent
   */
  mes: string;
  timer: any = null;
  /**
   *设备选择最大数量
   *
   * @type {number}
   * @memberof FarmOverhaulComponent
   */
  deviceCountLimit: number;
  /**
   *日期格式化
   *
   * @type {*}
   * @memberof FarmOverhaulComponent
   */
  en: any;
  constructor(
    private application: ApplicationService,
    private turbHistSer: TurboperHistoryService
  ) { }

  ngOnInit() {
    this.en = {
      firstDayOfWeek: 0,
      dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
      dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
      dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
      monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
      monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
      today: '今天',
      clear: '清除'
    };
    if (this.application.currentMenuParams && this.application.currentMenuParams['deviceCountLimit']) {
      this.deviceCountLimit = Number(this.application.currentMenuParams['deviceCountLimit'].paramValue);
    } else {
      this.deviceCountLimit = null;
    }
    this.farms = this.application.currentUserFarms;
    this.currentFarmId = this.farms[0].farmId;
  }

  /**
   * 风场点击事件
   * 
   * @param {Farm} item 
   * @memberof FarmOverhaulComponent
   */
  onFarmClick(item: Farm) {
    this.currentFarmId = item.farmId;
  }

  /**
   * 点击计算点非计算点
   * 
   * @param {number} n 
   * @memberof FarmOverhaulComponent
   */
  onClickHeader(n: number) {
    this.active = n;
  }

  /**
   * 风机选择返回的选中风机集合
   * 
   * @param {any} ev 
   * @memberof FarmOverhaulComponent
   */
  onSelectedDevicesChange(ev: Device[]) {
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      let deviceIdList: number[] = [];
      deviceIdList.push(...ev.map(device => device.deviceId));
      this.deviceIdList = deviceIdList;
      this.getData();
    }, 1000)
    // if(ev.length==this.turbineIdList.length)return false;

  }
  onSelectHistory(ev) {
    this.currentHistory = ev;

    if (!this.currentHistory) {
      this.mes = "没有选中数据"
      return this.display = true;
    }
    this.isEdit = true;

  }
  /**
   * 搜索事件
   * 
   * @memberof FarmOverhaulComponent
   */
  onSearch() {
    if (this.startDate > this.endDate) {
      this.mes = "开始时间不能大于结束时间"
      return this.display = true;
    }
    this.getData()


  }
  getData() {
    //非计划
    this.turbHistSer.getTurboperHistByTurbines(this.deviceIdList, this.startDate, this.endDate, "02")
      .then(res => {
        if (res) {
          this.turboperHistorys = [...res];
        }
      })
      .catch(err => {
        console.log(err);
      })
    //计划
    this.turbHistSer.getTurboperHistByTurbines(this.deviceIdList, this.startDate, this.endDate, "01")
      .then(res => {
        if (res) {
          this.turboperHistorys2 = [...res];
        }
      })
      .catch(err => {
        console.log(err);
      })
  }
  /**
   * 编辑当前数据
   * 
   * @memberof FarmOverhaulComponent
   */
  onEdit() {
    this.overhaultables.forEach((o, i) => {
      if (i == this.active) {
        o.emitSelector()
      }
    })
    //this.overhaultable.emitSelector()

  }
  onColseEdit(ev: boolean) {
    this.isEdit = false;
    if (ev) {
      this.mes = "数据更新成功"
      this.display = true;
      this.getData()
    }

    //更改后重新请求数据
  }
}



// WEBPACK FOOTER //
// ./src/app/module/data-analysis/farm-overhaul/farm-overhaul.component.ts